(
    function (factory) {
        if (typeof define === "function" && define.amd) {

            // AMD. Register as an anonymous module.
            define(["jquery", "./version"], factory);
        } else {

            // Browser globals
            factory(jQuery);
        }
    }
        (
        function ($) {

            //put app js code here
            //console.log("app template");

            var TableController = function () {
                var option = {
                    'showTable_area1': false,
                    'showTable_system1': false,
                    'showTable_time1': false,
                    'ta1_data': [],
                    'ta2_data': [],
                    'ta3_data': [],
                }
                //与页面交互的放在初始化里面
                var init = function () {
                    var op = option;

                    $("#c1").click(function () {

                        op.showTable_area1 = !op.showTable_area1
                        // if(op.showTable_area1)
                        //     op.showTable_area1 = false;
                        // else
                        //     op.showTable_area1 = true;
                        //   refresh()
                    });

                    $("#c2").click(function () {
                        op.showTable_system1 = !op.showTable_system1
                        //   refresh()
                    });
                    $("#c3").click(function () {
                        op.showTable_time1 = !op.showTable_time1
                        //    refresh()
                    });
                    $("#cTable").click(function () {
                        refresh()
                    });

                    $("#btn_t1").click(function(){
                        $('#t1').tableExport({
                            type: 'excel',
                            excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right']
                        });
                    })
                    $("#btn_t2").click(function(){
                        $('#t2').tableExport({
                            type: 'excel',
                            excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right']
                        });
                    })
                    $("#btn_t3").click(function(){
                        $('#t3').tableExport({
                            type: 'excel',
                            excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right']
                        });
                    })
                }

                var setData = function (data) {
                    this.option.ta1_data = data.ta1_data;
                    this.option.ta2_data = data.ta2_data;
                    this.option.ta3_data = data.ta3_data;
                }


                var refresh = function () {
                    var op = option;
                    if (op.showTable_area1)
                        $("#area1").css('display', 'block');
                    else
                        $("#area1").css('display', 'none');

                    if (op.showTable_system1)
                        $("#system1").css('display', 'block');
                    else
                        $("#system1").css('display', 'none');

                    if (op.showTable_time1)
                        $("#time1").css('display', 'block');
                    else
                        $("#time1").css('display', 'none');

                    datanav1 = op.ta1_data;
                    datanav2 = op.ta2_data;
                    datanav3 = op.ta3_data;

                    var clearTable1 = function () {
                        var ct = document.getElementById("t1");
                        ct.innerHTML = "";
                    }

                    var clearTable2 = function () {
                        var ct = document.getElementById("t2");
                        ct.innerHTML = "";
                    }
                    var clearTable3 = function () {
                        var ct = document.getElementById("t3");
                        ct.innerHTML = "";
                    }
                    var table = document.getElementsByClassName("ta1")[0];
                    clearTable1();
                    for (var i = 0; i < datanav1.length; i++) {
                        var tr = table.insertRow(table.rows.length);
                        var obj = datanav1[i];
                        for (var p in obj) {
                            var td = tr.insertCell(tr.cells.length);
                            td.innerText = obj[p];
                        }
                    }

                    var table = document.getElementsByClassName("ta2")[0];
                    clearTable2();
                    for (var i = 0; i < datanav2.length; i++) {
                        var tr = table.insertRow(table.rows.length);
                        var obj = datanav2[i];
                        for (var p in obj) {
                            var td = tr.insertCell(tr.cells.length);
                            td.innerText = obj[p];
                        }
                    }
                    var table = document.getElementsByClassName("ta3")[0];
                    clearTable3();
                    for (var i = 0; i < datanav3.length; i++) {
                        var tr = table.insertRow(table.rows.length);
                        var obj = datanav3[i];
                        for (var p in obj) {
                            var td = tr.insertCell(tr.cells.length);
                            td.innerText = obj[p];
                        }
                    }
                }
                //把私有变量变成从外部可访问
                this.option = option; 
                this.init = init;
                this.setData = setData;
            }


            var tableData = function () {
                var tc = new TableController();
                // console.log(data);  test
                $.get('mockdata/table.json', function (data) {
                    tc.init();
                    tc.setData(data);
                });

            }
            $(tableData)

        }


        )


)